<template>
    <div class="padding20">
        <!--面包屑导航区域-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!--卡片视图-->
        <el-card>
            <!--搜索组件 + 添加按钮-->
            <el-row>
                <el-col :span="8">
                    <el-input placeholder="请输入内容" 
                        v-model="queryInfo.query"
                        clearable
                        @clear="getGoodsList"
                        @keyup.enter.native="getGoodsList">
                        <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button class="marginLeft20" type="primary" @click="toAddGoodPage">添加</el-button>
                </el-col>
            </el-row>

            <!--表格-->
            <el-table :data="goodsList" border stripe>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column label="商品名称" prop="goods_name"></el-table-column>
                <el-table-column label="产品价格" prop="goods_price" width="100px"></el-table-column>
                <el-table-column label="商品重量" prop="goods_weight" width="100px"></el-table-column>
                <el-table-column label="创建时间" prop="add_time" width="200px">
                    <template slot-scope="scope">
                        {{scope.row.add_time | timeStampFormat }}
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="130px">
                    <template slot-scope="scope">
                        <!--修改-->
                        <el-tooltip  effect="dark" content="修改信息" placement="top" :enterable="false">
                            <el-button type="success" size="mini" icon="el-icon-setting" 
                                @click="handleShowEditDialog(scope.row)"></el-button>
                        </el-tooltip>
                        <!--删除-->
                        <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
                            <el-button type="danger"  size="mini" icon="el-icon-delete" 
                                @click="deleteGood(scope.row.goods_id)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页组件-->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[2,5,10]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="goodsListTotal">
            </el-pagination>
        </el-card>
    </div>
</template>

<script>
import {handleGetGoodsList,handleDelGoodsById} from '@/request/goodList'
export default {
    props: {

    },
    components: {

    },
    data() {
        return {
            queryInfo:{
                query:'',
                pagenum: 1,
                pagesize: 5
            },
            goodsList:[],
            goodsListTotal:0
        };
    },
    computed: {

    },
    created() {
        this.getGoodsList()
    },
    mounted() {

    },
    watch: {

    },
    methods: {
        /*
            跳转到添加商品页面
        */
        toAddGoodPage(){
            this.$router.push('/goods/add')
        },
        getGoodsList(){
            handleGetGoodsList(this.queryInfo)
            .then(res=>{
                if(res.meta.status!==200){
                    return this.$message.error('获取商品列表失败')
                }else{
                    this.goodsList = res.data.goods
                    this.goodsListTotal = res.data.total
                }
            })
        },
        /*
            监听pageSize改变
        */
        handleSizeChange(newSize){
            this.queryInfo.pagesize = newSize
            this.getGoodsList()
        },
        /*
            监听页码值page改变
        */
        handleCurrentChange(newPage){
            this.queryInfo.pagenum = newPage
            this.getGoodsList()
        },
        /*
            删除商品
        */
        async deleteGood(id){
            let delResult = await this.$confirm(`确定删除该商品吗?`,'提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
            .catch(err=>err)
            // 取消是cancel  确定是confirm
            if(delResult==='confirm'){
                handleDelGoodsById(id)
                .then(res=>{
                    if(res.meta.status!==200){
                        this.$message.error(res.meta.msg)
                    }else{
                        this.$message.success(res.meta.msg)
                        this.getGoodsList()  //刷新数据
                    }
                })
            }
        }
    },
};
</script>

<style scoped lang="less">

</style>
